package web.filter;

import java.io.IOException;
import java.util.List;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.filter.OncePerRequestFilter;

public class UrlFilter extends OncePerRequestFilter {

	private List<String> pageList;

	public void setPageList(List<String> pageList) {
		this.pageList = pageList;
	}

	protected void doFilterInternal(HttpServletRequest request,	HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
		if (isPageAllowed(request)) {
			chain.doFilter(request, response);
		}
	}

	private boolean isPageAllowed(HttpServletRequest request) {
		boolean isAllowed = false;

		if (request.getRequestURI().equals(request.getContextPath() + "/")) {
			isAllowed = true;
		}
		else {
			for (int index = 0; index < pageList.size(); index++) {
				if (request.getRequestURI().endsWith((String) pageList.get(index))) {
					isAllowed = true;
					break;
				}
			}
		}
		return isAllowed;
	}
}